package com.nidu.demo.web.system;

import com.alibaba.cola.dto.MultiResponse;
import com.alibaba.cola.dto.PageResponse;
import com.alibaba.cola.dto.Response;
import com.alibaba.cola.dto.SingleResponse;
import com.alibaba.fastjson2.JSONArray;
import com.nidu.demo.base.BatchDeleteCmd;
import com.nidu.demo.common.enums.StatusEnum;
import com.nidu.demo.permission.api.SysPermissionService;
import com.nidu.demo.role.dto.RoleMenuAssignCmd;
import com.nidu.demo.role.api.SysRoleService;
import com.nidu.demo.role.dto.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashSet;
import java.util.List;

/**
 * 角色管理 Controller
 */
@Tag(name = "管理后台 - 角色")
@RestController
@RequestMapping("/system/role")
@Validated
@RequiredArgsConstructor
public class SysRoleController {

    private final SysRoleService service;

    private final SysPermissionService permissionService;

    @Operation(summary = "创建角色", description = "创建新的角色")
    @PostMapping
    @PreAuthorize("@ss.hasPermission('system:role:create')")
    public SingleResponse<RoleCO> addRole(@RequestBody RoleAddCmd cmd) {
        return service.add(cmd);
    }

    @Operation(summary = "更新角色", description = "更新角色的信息")
    @PutMapping("/{id}")
    @PreAuthorize("@ss.hasPermission('system:role:update')")
    public SingleResponse<RoleCO> updateRole(@PathVariable Long id, @RequestBody RoleUpdateCmd cmd) {
        cmd.setId(id);
        return service.update(cmd);
    }

    @Operation(summary = "删除角色", description = "根据 ID 删除角色")
    @DeleteMapping("/{id}")
    @PreAuthorize("@ss.hasPermission('system:role:delete')")
    public Response deleteRole(@PathVariable Long id) {
        return service.delete(id);
    }

    @Operation(summary = "批量删除角色", description = "根据 ID 列表删除多个角色")
    @PostMapping("/batch")
    @PreAuthorize("@ss.hasPermission('system:role:delete')")
    public Response deleteRoles(@RequestBody BatchDeleteCmd cmd) {
        List<Long> ids = JSONArray.parseArray(cmd.getDelete(), Long.class);
        return service.delete(new HashSet<>(ids));
    }

    @Operation(summary = "获取角色详情", description = "根据 ID 获取角色详细信息")
    @GetMapping("/{id}")
    @PreAuthorize("@ss.hasPermission('system:role:query')")
    public SingleResponse<RoleCO> getRoleById(@PathVariable Long id) {
        return service.getById(id);
    }

    @Operation(summary = "查询角色列表", description = "获取所有角色信息")
    @GetMapping("/list")
    @PreAuthorize("@ss.hasPermission('system:role:query')")
    public MultiResponse<RoleCO> listRoles(RoleListQry qry) {
        return service.list(qry);
    }

    @Operation(summary = "分页查询角色", description = "分页获取角色信息")
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPermission('system:role:query')")
    public PageResponse<RoleCO> pageRoles(RolePageQry qry) {
        return service.page(qry);
    }

    @Operation(summary = "更新角色状态", description = "更新角色的启用/禁用状态")
    @PutMapping("/{id}/status")
    @PreAuthorize("@ss.hasPermission('system:role:update')")
    public Response updateStatus(@PathVariable Long id, @RequestParam StatusEnum status) {
        return service.updateStatus(id, status);
    }

    @Operation(summary = "查询角色菜单ID列表", description = "查询角色菜单ID列表")
    @GetMapping("/{roleId}/menus")
    public MultiResponse<Long> getRoleMenus(@PathVariable Long roleId) {
        return permissionService.getRoleMenus(roleId);
    }

    @Operation(summary = "分配菜单权限", description = "给角色分配菜单权限")
    @PostMapping("/{roleId}/menus")
    public Response assignRoleMenu(@PathVariable Long roleId, @RequestBody RoleMenuAssignCmd cmd) {
        cmd.setRoleId(roleId);
        return permissionService.assignRoleMenu(cmd);
    }
}